<!--
 * @Author: 赵克强 huohuo625@163.com
 * @Date: 2023-10-17 10:13:29
 * @LastEditors: 赵克强 huohuo625@163.com
 * @LastEditTime: 2025-04-14 16:59:29
 * @FilePath: \hy-ui\src\views\workflow\work\start.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container" v-loading="loading">
    <div class="startYcCss">
      <div style="margin-bottom:10px;height:63px; position: sticky;
  top: 0;z-index: 1;">
        <el-card class="box-card">
          <div style="display: flex;align-items: cneter;justify-content: space-between;">
            <span style="font-size: 14px; font-weight: 600">发起流程</span>
            <div>
              <!-- <el-button @click="handleReset" v-if="formConfig.formConfig && formConfig.widgetList" size="mini"
                icon="el-icon-refresh">重置</el-button> -->
              <el-button type="warning" @click="handleSubmit('1')" v-if="formConfig.formConfig && formConfig.widgetList"
                icon="el-icon-folder-checked" size="mini">保
                存</el-button>
              <el-button type="primary" @click="handleSubmit('2')" v-if="formConfig.formConfig && formConfig.widgetList"
                icon="el-icon-check" size="mini">提
                交</el-button>
              <el-button @click="handleBack" type="primary" size="mini" icon="el-icon-close">返回</el-button>
            </div>
          </div>
        </el-card>
      </div>
      <div class="formCssCard">
        <el-card class="box-card">
          <el-col :span="18" :offset="3" style="padding-bottom:20px;">
            <div class="main">
              <div class="form-conf">
                <v-form-render ref="vFormRef" :option-data="formConfig.dictList">
                </v-form-render>
              </div>
            </div>
          </el-col>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { getProcessForm, startProcess } from "@/api/workflow/process";
import { getNodeInterface } from "@/api/workflow/model";
import {
  appDictData,
  frontViewData,
  saveOrUpdate,
} from "@/api/render/pager.js";
import request from "@/utils/request";
export default {
  name: "WorkStart",
  data() {
    return {
      definitionId: null,
      deployId: null,
      procInsId: null,
      formOpen: false,
      formConfig: {},
      detailId: undefined,
      apiList: {},
      cronTaskId: '',
      loading: false,
    };
  },
  created() {
    this.initData();
  },
  methods: {
    async initData() {
      this.deployId = this.$route.params && this.$route.params.deployId;
      this.definitionId = this.$route.query && this.$route.query.definitionId;
      this.procInsId = this.$route.query && this.$route.query.procInsId;
      this.detailId = this.$route.query && this.$route.query.id;
      this.cronTaskId = this.$route.query && this.$route.query.cronTaskId;
      getProcessForm({
        definitionId: this.definitionId,
        deployId: this.deployId,
        procInsId: this.procInsId,
      }).then(async (res) => {
        if (res.data) {
          let modalConfig = {};
          modalConfig = {
            ...res.data,
            formConfig: JSON.parse(res.data.formConfig),
            widgetList: JSON.parse(res.data.widgetList),
          };
          this.formConfig = modalConfig;
          let formJson = {
            formConfig: this.formConfig?.formConfig,
            widgetList: this.formConfig?.widgetList,
          };
          this.$refs.vFormRef.setFormJson(formJson);
          if (!!modalConfig.formConfig.addInitInterface && !this.detailId) {
            const viewData = await request({
              url: modalConfig.formConfig.addInitInterface,
              method: "get",
            });
            modalConfig.detailData = { ...viewData.data };
          }
          if (!!this.detailId) {
            const detailRes = await frontViewData(
              this.formConfig?.formConfig?.tableName,
              {
                id: this.detailId,
              }
            );
            if (detailRes.code === 200) {
              modalConfig.detailData = { ...detailRes.data };
            }
          }
          this.$nextTick(() => {
            if (modalConfig?.detailData) {
              this.$refs.vFormRef.setFormData(modalConfig?.detailData);
            }
          });
        }
        const apiRes = await getNodeInterface({ taskId: res.data?.nodeKey });
        if (apiRes.rows?.length) {
          apiRes.rows?.forEach((item) => {
            this.apiList[item.optType] = item.apiUrl;
          });
        }
      });
    },
    async handleSubmit(type) {
      if (type === "1") {
        const params = await this.$refs.vFormRef.getFormData();
        if (this.formConfig.formConfig.erMark) {
          params.erMark = this.formConfig.formConfig.erMark;
        }
        if (!!this.detailId) {
          params.id = this.detailId;
        }
        this.loading = true
        if (!!this.apiList?.["save"]) {
          const beforeRes = await request({
            url: this.apiList["save"],
            method: "post",
            data: params,
          });
          if (beforeRes.code != 200) {
            this.$message.error(beforeRes.message || beforeRes.msg);
            return;
          }
        }
        const res = await saveOrUpdate(
          this.formConfig?.formConfig?.tableName,
          params
        );
        if (res.code === 200) {
          this.$message.success("保存成功");
          this.$refs.vFormRef.resetForm();
          this.$tab.closePage();
          this.$router.go(-1);
        }
        this.loading = false
      } else {
        if (this.formConfig.formConfig?.onFormSaveBefore) {
          new Function(this.formConfig.formConfig?.onFormSaveBefore)();
        }
        this.$refs.vFormRef.validateForm(async (valid) => {
          if (valid) {
            this.loading = true
            const data = await this.$refs.vFormRef.getFormData();
            if (!!this.detailId) {
              data.id = this.detailId;
            }
            const params = {
              tableName: this.formConfig.formConfig.tableName,
              obj: data,
              variables: {
                dictList: this.formConfig?.dictList,
                ...data,
              },
            };
            if (!!this.cronTaskId) {
              params.cronTaskId = this.cronTaskId;
            }
            if (this.formConfig.formConfig.erMark) {
              params.erMark = this.formConfig.formConfig.erMark;
            }
            if (!!this.apiList?.["submit"]) {
              const beforeRes = await request({
                url: this.apiList["submit"],
                method: "post",
                data: params,
              });
              if (beforeRes.code != 200) {
                this.$message.error(beforeRes.message || beforeRes.msg);
                return;
              }
            }
        const res = await startProcess(this.definitionId, params);
        if (res.code === 200) {
          if (this.formConfig.formConfig?.onFormSaveAfter) {
            new Function(this.formConfig.formConfig?.onFormSaveAfter)();
          }
          this.$message.success(res.msg);
          this.$refs.vFormRef.resetForm();
          this.$tab.closePage();
          this.$router.go(-1);
        }
        this.loading = false
      }
    });

  }
},
handleReset() {
  this.$refs.vFormRef.resetForm();
},
handleBack() {
  this.$tab.closePage();
  this.$router.go(-1);
},
  },
};


</script>

<style lang="scss" scoped>
.form-conf {
  margin: 10px auto;
  width: 90%;
  min-width: 1000px;

  ::v-deep .el-form-item__error {
    z-index: 5;
  }
}

::v-deep .el-card {
  // min-height: 88vh;
}

.main {
  min-height: 80vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border: 1px #f2f2f2 solid;
  padding-bottom: 20px;


  .btn {
    display: flex;
    justify-content: center;
    margin-bottom: 15px;
  }
}

.startYcCss {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 90px);
  overflow-y: auto;
  width: 100%;

  .formCssCard {
    flex: 1;
  }
}
</style>
